<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Users extends CI_Controller {

	public function index()
	{
		$this->load->helper('form');
		$this->load->helper('url');
		
		$this->load->model('UsersModel');
		$data['users'] = $this->UsersModel->select_all();
		$this->load->view('view_users', $data);
	}
	
	public function view_users()
	{
		$this->load->helper('form');
		$this->load->helper('url');
		
		$this->load->model('UsersModel');
		$data['users'] = $this->UsersModel->select_all();
		$this->load->view('view_users', $data);
	}
	
	public function view_user()
	{
		$this->load->helper('form');
		$this->load->helper('url');
		
		$this->load->model('UsersModel');
		$data['user'] = $this->UsersModel->select_user($_GET['id']);
		$this->load->view('view_user', $data);
	}
	
	public function update_user()
	{
		$this->load->helper('form');
		$this->load->helper('url');
		
		$this->load->model('UsersModel');
		$data['user'] = $this->UsersModel->select_user($_GET['id']);
		$this->load->view('update_user', $data);
	}
	
	public function update_data()
	{
		$this->load->helper('form');
		$this->load->helper('url');
		$this->load->model('UsersModel');
		$this->load->database();
		$this->load->helper(array('form', 'url'));

		$this->load->library('form_validation');
		$this->form_validation->set_rules('user_firstname', 'Firstname', 'required|alpha_dash');
		$this->form_validation->set_rules('user_middlename', 'Middlename', 'required|alpha_dash');
		$this->form_validation->set_rules('user_lastname', 'Lastname', 'required|alpha_dash');
		$this->form_validation->set_rules('user_email', 'Email', 'required|valid_email');
		$this->form_validation->set_rules('user_cellphone', 'Cellphone', 'alpha_dash');
		$this->form_validation->set_rules('user_city', 'City', 'alpha_dash');
		$this->form_validation->set_rules('user_hometown', 'Hometown', 'alpha_dash');
		$this->form_validation->set_rules('user_country', 'Country', 'alpha_dash');
		
		if ($this->form_validation->run() == FALSE)
		{
			//redirect(base_url() . 'index.php/users/update_user?&id=' . $_POST['user_id'],'refresh');
			redirect('users');
			//redirect(base_url() . 'index.php/users/update_user?&id=' . $_POST['user_id'],'refresh')
			//redirect(base_url() . 'index.php/users/update_user?&id=' . $_POST['user_id'],'refresh');
			$data['id'] = $_POST['user_id'];
			$data['user'] = $this->UsersModel->select_user($_POST['user_id']);
			$this->load->view('update_user',$data);
			//redirect('users');
		}
		else
		{
			$this->UsersModel->update_data();
			redirect(base_url() . 'index.php/users/view_user?&id=' . $_POST['user_id'],'refresh');
		}
	}
	
	public function insert_photo()
	{
	$this->load->helper('form');
	$this->load->helper('url');
	
	
	 $fileName = $_FILES['userfile']['name'];
	 $tmpName = $_FILES['userfile']['tmp_name'];
	 $fileSize = $_FILES['userfile']['size'];
	 $fileType = $_FILES['userfile']['type'];
	 $fp      = fopen($tmpName, 'r');
	 $content = fread($fp, filesize($tmpName));
	 fclose($fp);
		 if(!get_magic_quotes_gpc())
		 {
			$fileName = addslashes($fileName);
		 }

		 $query = "INSERT INTO photo set photo =" . $content . " user_id = " . $_POST['user_id'] . "  WHERE user_id =".$_POST['user_id']."";
		 mysql_query($query);
		
		$this->load->model('UsersModel');
		$this->db->where('user_id', $_POST['user_id']);
		$this->UsersModel->insert_photo();
		$data['user'] = $this->UsersModel->select_user($_GET['id']);
		$this->load->view('view_user', $data);
		
	}
	
	public function loginHandler()
	{
	
		$this->load->database();
	
		$username = $_POST['username'];
		$password = md5($_POST['password']);
			
		$query = $this->db->get_where('login', array('username' => $username));
		
		$user = $query->row();
		if($user->username == $username){
			if($user->password == $password){
	
				$sesh_vals = array(
				 'username'  => $username,
				 'logged_in' => TRUE
				 );

				$this->session->set_userdata($sesh_vals);
				$session_id = $this->session->userdata('username');
				
					redirect('homepage','refresh');
				
			}else{
			
				echo "Wrong Password";
				redirect('home','refresh');
			}
		}else{
			echo "Wrong Username";
			redirect('home','refresh');
		}
	
	}
	
	public function logoutHandler(){
			$this->session->sess_destroy();
			$this->load->view('home','refresh');
	}
	
	public function createUser(){
		$this->load->model('UsersModel');
		$data['create'] = $this->UsersModel->insert_data(); 
	}
}
	
